package com.uestc.soft.service;

import com.uestc.soft.mapper.LoopholeMapper ;
import com.uestc.soft.pojo.Loophole ;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LoopholeService {

    @Resource
    private LoopholeMapper loopholeMapper;

    public Map<String, Object> insert(Loophole loophole) {
        Timestamp timestamp = new Timestamp(System.currentTimeMillis()); // 写入一个注册时间 new Date()代表当前服务器系统时间
        loophole.setRegTime(timestamp);

        String level = loophole.getLevel();
        String loopholename = loophole.getLoopholename();
        Integer number = loophole.getNumber();

        loopholeMapper.insert(loophole);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "新增漏洞成功");
        return map;
    }

    public Map<String, Object> deleteById(Integer id) {
        loopholeMapper.deleteById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "删除漏洞成功");
        return map;
    }

    public Map<String, Object> updateLevelAndNumber(Integer id, Integer number ) {
        loopholeMapper.updateLevelAndNumber(id, number);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "更新漏洞成功");
        return map;
    }

    public Map<String, Object> selectByPage(int page, int limit, String level, String number) {
        // page 页码   (page-1)*limit=起始下标index
        int index = (page - 1) * limit;   // 计算出起始下标
        List<Loophole> loopholeList = loopholeMapper.selectByPage(index, limit, level ,number);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "查询成功");
        map.put("count", loopholeMapper.selectCount(level ,number));
        map.put("data", loopholeList);
        return map;
    }
}
